In the Draw method you want to draw on the display. But that’s all you want to do. If you need to perform some calculations in preparation for drawing, you should do those in the Update method. The Update method prepares the program for the Draw method. Very often an XNA program will be moving sprites around the display based on user
input. For the phone, this user input mostly involves fingers touching
the screen. All handling of user input should also occur during the Update method.
You should write your Update and Draw
methods so that they execute as quickly as possible. That’s rather
obvious, I guess, but here’s something very important that might not be
so obvious:
You should avoid code in Update and Draw that routinely allocates memory from the program’s local heap.
Eventually the .NET garbage collector will want to reclaim some of this
memory, and while the garbage collector is doing its job, your game
might stutter a bit. Throughout the chapters on XNA programming, you’ll
see techniques to avoid allocating memory from the heap.
Your Draw methods probably won’t contain any questionable code; it’s usually in the Update method where trouble lurks. Avoid any new
expressions involving classes. These always cause memory allocation.
Instantiating a structure is fine, however, because structure instances
are stored on the stack and not in the heap. (XNA uses structures rather than classes for many types of objects you’ll often need to create in Update.) But heap allocations can also occur without explicit new
expressions. For example, concatenating two strings creates another
string on the heap. If you need to perform string manipulation in Update, you should use StringBuilder. Conveniently, XNA provides methods to display text using StringBuilder objects.
In XnaHelloPhone, however, the Update method is trivial. The text displayed by the program is anchored in one spot. All the necessary calculations have already been performed in the LoadContent method. For that reason, the Update method will be left simply as XNA Game Studio originally created it:
Example 6. XNA Project: XnaHelloPhone File: Game1.cs (excerpt)
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();
base.Update(gameTime); }
|
The default code uses the static GamePad class to check if the phone’s hardware Back button has been pressed and uses that to exit the game.
Finally, there is the Draw method. The version created for you simply colors the background with a light blue:
Example 7. XNA Project: XnaHelloPhone File: Game1.cs (excerpt)
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime); }
|
The color known as CornflowerBlue
has achieved iconic status in the XNA programming community. When
you’re developing an XNA program, the appearance of the light blue
screen is very comforting because it means the program has at least
gotten as far as Draw.
But if you want to conserve power on OLED displays, you want to go with
darker backgrounds. In my revised version, I’ve compromised by setting
the background to a darker blue. As in Silverlight, XNA supports the 140
colors that have come to be regarded as standard. The text is colored
white:
Example 8. XNA Project: XnaHelloPhone File: Game1.cs (excerpt)
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Navy);
spriteBatch.Begin(); spriteBatch.DrawString(segoe14, text, textPosition, Color.White); spriteBatch.End();
base.Draw(gameTime); }
|
Sprites get out on the display by being bundled into a SpriteBatch object, which was created during the call to LoadContent. Between calls to Begin and End there can be multiple calls to DrawString to draw text and Draw to draw bitmaps. Those are the only options. This particular DrawString
call references the font, the text to display, the position of the
upper-left corner of the text relative to the upper-left corner of the
screen, and the color. And here it is:
Oh, that’s interesting! By default,
Silverlight programs come up in portrait mode, but XNA programs come up
in landscape mode. Let’s turn the phone or emulator sideways: